package com.maxima.attendance.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.maxima.attendance.daos.ProjectDAO;
import com.maxima.attendance.pojos.Project;
import com.opensymphony.xwork2.ActionSupport;

public class ProjectAction extends ActionSupport implements SessionAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Project project = new Project();
	private Integer id;
	private Map<String, Object> map;
	// for logging
	static Logger logger = Logger.getLogger(ProjectAction.class.getName());
	List<Project> proList = new ProjectDAO().getAll();
	List<Project> projectList = new ArrayList<Project>();

	public ProjectAction() {}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public String addProject() {
		String projectName = ServletActionContext.getRequest().getParameter("project_name");
		String projectAgreement = ServletActionContext.getRequest().getParameter("project_agr");
		for (Project project : proList) {
			projectList.add(new Project(project));
		}
		for (Project project : projectList) {
			if (project.getAgreement().equals(projectAgreement)) {
				System.out.println("same agreement found");
				return ERROR;
			}
		}
		this.project.setProjectName(projectName);
		this.project.setAgreement(projectAgreement);
		new ProjectDAO().add(this.project);
		logger.info("New Project - " + this.project.getProjectName() + " Id - " + this.project.getId() + " - added Successfully");
		return SUCCESS;
	}

	public String selectProject() throws Exception {
		map.put(SessionManager.SELECTED_PROJECT, new ProjectDAO().get(this.getId()));
		this.setSession(map);
		return SUCCESS;
	}

	public String updateProject() throws Exception {
		ProjectDAO projectDAO = new ProjectDAO();
		int projectId = ((Project) this.getMap().get(SessionManager.SELECTED_PROJECT)).getId();
		String projectName = ServletActionContext.getRequest().getParameter("project_name");
		String projectAgreement = ServletActionContext.getRequest().getParameter("project_agr");
		for (Project project : proList) {
			projectList.add(new Project(project));
		}
		for (Project project : projectList) {
			if (!project.getId().equals(projectId)) {
				if (project.getAgreement().equals(projectAgreement)) {
					System.out.println("Same project found");
					return ERROR;
				}
			}
		}
		Project project = projectDAO.get(projectId);
		project.setProjectName(projectName);
		project.setAgreement(projectAgreement);
		projectDAO.update(projectId, project);
		logger.info("Holiday - " + project.getProjectName() + " Id - " + projectId + " - updated Successfully");
		map.put(SessionManager.SELECTED_PROJECT, null);
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> map) {
		this.map = map;
	}

	public String clearSelectedProject() {
		map.put(SessionManager.SELECTED_PROJECT, null);
		return SUCCESS;
	}
}
